<?php

/**
 * @author	barbarosalcin
 * @desc	page_admin_stats
 * @version	$Id: 20110317
 * @package	admin
 */

if ($tk->session->data['user_level'] != ADMIN)
{
	$allowed = 0;
	return;
}

$filename = basename(__FILE__);
$pagename = substr(substr($filename, 0, - 4), 5, 99);
define('IN_ADMINPAGE',true);

/* get list */

if (! empty($setmodules))
{
	$modules[$pagename]['manitem'] = $req['language'] . '-' . $pagename . $config['seoext'] . '?act=manitem';
	return;
}

if ($req['get']['act'] == 'manitem')
{
	/**
	 * @uses text,select,time 
	 */
	
	$search_array = array(
			'date_select' => array(
					'date' => 'time'), 
			'arr' => array());
	
	$thisdate = ! ($req['get']['date'] != '') ? date('Y-m-d', $tk->config['time']) : date('Y-m-d',$tk->f->date2int($req['get']['date']));
	
	if ($config['analytics_pass'] != '' && $config['analytics_table'] != '' && $config['analytics_pass'] != '')
	{
		
		$ga = new gapi($config['analytics_user'], $config['analytics_pass']);
		
		$ga->requestReportData($config['analytics_table'], array(
				'date', 
				'pagePath'), array(
				'visits'), array(
				'-visits'), null, $thisdate, $thisdate, 1, 30);
		
		$i = 0;
		unset($data_json);
		foreach ( $ga->getResults() as $result )
		{
			
			$dimensions = $result->getDimensions();
			$metrics = $result->getMetrics();
			
			$data_json['p' . $i] = array(
					
					'id' => date('U', mktime(0, 0, 0, substr($thisdate, 4, 2), substr($thisdate, 6, 2), substr($thisdate, 0, 4))), 
					'visits' => $metrics['visits'], 
					'date' => $dimensions['date'], 
					'pagePath' => $dimensions['pagePath']);
			
			$i++;
		}
		if ((int) $req['get']['s'])
		{
			$tk->page->_pv['json'] = 1;
			$tk->page->_pv['data_json'] = preg_replace('%\[(.+)\]%', '$1', json_encode($data_json));
		}
		else
		{
			
			$tk->page->_pv['data']['list'] = $data_json;
			
			unset($data_json);
			$tk->page->_pv['search'] = $search_array;
		}
	}
	/* default ! do not edit */
	$tk->page->_pv['data']['act'] = $req['get']['act'];
	$tk->page->_pv['data'][$req['get']['act']] = $req['language'] . '-' . $pagename . '.html?';
}